// pages/coulumnList/coulumnList.js

const app = getApp();
var api = require('../../api.js');
var mta = require('../../mta_analysis.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    arr: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var page = this;
    app.bind();
    page.getDetail();
    wx.getNetworkType({
      success: function (res) {
        console.log(res)
        page.setData({
          networkType: res.networkType
        })
      }
    });
    mta.Page.init();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var url = app.getcurrentPage();
    wx.setStorageSync("from_url", url);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    // that.setData({
    //   page: that.data.count - 1
    // });
    // that.getDetail();

    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();

    //停止下拉刷新
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    // 显示加载图标  
    wx.showLoading({
      title: '玩命加载中',
    });

    if (that.data.page < that.data.count) {
      that.setData({
        page: that.data.page + 1
      });
    }

    that.getDetail();
    // 隐藏加载框  
    wx.hideLoading();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var url = app.getcurrentPage();
    var user_id = wx.getStorageSync('user_id');
    return {
      path: url + "?user_id=" + user_id,
      title: '得道1314'
    };
  },

  //获取列表
  getDetail: function () {
    var access_token = wx.getStorageSync('access_token');
    console.log(access_token)
    var that = this;
    wx.request({
      url: api.index.wendao,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        page: that.data.page
      },

      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          that.setData({
            list: res.data.data.wendao_list,
          })
         
        } else if (res.data.code == -1) {
          wx.navigateTo({
            url: '/pages/grant/grant',
          })
        }
      }
    });

  },

  //跳转到课程专栏列表
  toCourse: function (e) {
    var page = this;
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var list = page.data.list;
    var lessonId = list[index].id;
    let str = JSON.stringify(list[index]);
    console.log(str);
    wx.navigateTo({
      url: '../../pages/wendaoList/wendaoList?lessonId=' + lessonId,
    })
  },

  //跳转到首页
  toIndex: function () {
    wx.switchTab({
      url: '/pages/index/index'
    })
  }
})